package com.sweets.look.preference;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

import com.sweets.look.constant.Constants;
import com.sweets.look.ui.App;

public class PreferenceManager {
    public class Name {
        public static final String ADDON = "addon_";
        public static final String ADDONS_SCREENSHOT = "addonscreenshot";
        public static final String ADDONS_TRANSLATE = "addontranslate";
        public static final String ADDONS_VIEW_SOURCE = "addonviewsource";
        public static final String ADOBE_FLASH_SUPPORT = "enableflash";
        public static final String ANIM = "anim";
        public static final String BLOCK_3RD_PARTY_COOKIES = "block3rdpartycookies";
        public static final String BLOCK_ADS = "AdBlock";
        public static final String BLOCK_IMAGES = "blockimages";
        public static final String BOOKMARKS_STYLE = "bookmarksStye";
        public static final String CHANGELOG_CODE = "changelogcode";
        public static final String CLEAR_CACHE_EXIT = "cache";
        public static final String CLEAR_COOKIES_EXIT = "clearCookiesExit";
        public static final String CLEAR_FORMDATA_EXIT = "clearFormdataExit";
        public static final String CLEAR_HISTORY_EXIT = "clearHistoryExit";
        public static final String CLEAR_STORAGE_EXIT = "clearStorageExit";
        public static final String CODE = "code";
        public static final String COLOR_MODE = "colormode";
        public static final String COOKIES = "cookies";
        public static final String DOWNLOAD_DIRECTORY = "download";
        public static final String DO_NOT_TRACK = "donottrack";
        public static final String FULL_SCREEN = "fullscreen";
        public static final String GESTURE_TOOLBAR_LEFT = "gesturetoolbarleft";
        public static final String GESTURE_TOOLBAR_RIGHT = "gesturetoolbarright";
        public static final String HIDE_STATUS_BAR = "hidestatus";
        public static final String HIDE_UNDERLINE = "hideunderline";
        public static final String HOMEPAGE = "home";
        public static final String IMAGES_SWITCH = "imageswitch";
        public static final String INCOGNITO_COOKIES = "incognitocookies";
        public static final String INCOGNITO_MODE = "incognitoMode";
        public static final String JAVASCRIPT = "java";
        public static final String KEY_BACK = "keyback";
        public static final String KEY_FORWARD = "keyforward";
        public static final String KEY_HOME = "keyhome";
        public static final String KEY_MENU = "keymenu";
        public static final String KEY_TAB = "keytab";
        public static final String LINKS_TIMES = "linkstimes";
        public static final String LOCAL_ADB = "localAdb";
        public static final String LOCATION = "location";
        public static final String LOG_IN = "login";
        public static final String NIGHT_MODE = "nightmode";
        public static final String OPEN_TIMES = "opentimes";
        public static final String OVERVIEW_MODE = "overviewmode";
        public static final String POPUPS = "newwindows";
        public static final String QUICK_BACK = "quickback";
        public static final String REMOVE_IDENTIFYINF_HEADERS = "removeidentifyingheaders";
        public static final String RENDERING_MODE = "renderMode";
        public static final String REQUEST_DESKTOP_SITE = "requestdesktopsite";
        public static final String RESTORE_LOST_TABS = "restoreclosed";
        public static final String SAVE_FORMDATA = "saveformdata";
        public static final String SAVE_PASSWORDS = "passwords";
        public static final String SCREEN_ORIENTATION = "screenOrientation";
        public static final String SEARCH = "search";
        public static final String SEARCH_HINT = "searchhint";
        public static final String SEARCH_TIMES = "searchtimes";
        public static final String SEARCH_URL = "searchurl";
        public static final String TEXT_ENCODING = "textencoding";
        public static final String TEXT_REFLOW = "textreflow";
        public static final String TEXT_SIZE = "textsize";
        public static final String UI = "ui";
        public static final String URL_BOX = "urlbox";
        public static final String URL_MEMORY = "memory";
        public static final String USER_AGENT = "agentchoose";
        public static final String USER_AGENT_STRING = "userAgentString";
        public static final String USER_NAME = "username";
        public static final String USER_PSW = "userpsw";
        public static final String USE_WIDE_VIEWPORT = "wideviewport";
        public static final String VOLUME = "volume";
        public static final String WAKE_LOCK = "wakelock";
        public static final String WEB_INTENT = "webintent";

        public static final String APP_FIRST_LAUNCHER = "app_first_launcher";
    }

    private static final String PREFERENCES = "settings";
    private static PreferenceManager mInstance;
    private static SharedPreferences mPrefs;

    private PreferenceManager() {
        super();
        mPrefs = App.getAppContext().getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
    }

    public boolean isFirstLauncher() {
        return mPrefs.getBoolean(Name.APP_FIRST_LAUNCHER, true);
    }

    public void setFirstLauncher(boolean isFirst) {
        putBoolean(Name.APP_FIRST_LAUNCHER, isFirst);
    }

    public boolean getAdBlockEnabled() {
        return mPrefs.getBoolean(Name.BLOCK_ADS, true);
    }

    public boolean getAddonsEnabled(String packageName) {
        return mPrefs.getBoolean(Name.ADDON + packageName, true);
    }

    public boolean getAddonsScreenshotEnabled() {
        return mPrefs.getBoolean(Name.ADDONS_SCREENSHOT, true);
    }

    public boolean getAddonsTranslateEnabled() {
        return mPrefs.getBoolean(Name.ADDONS_TRANSLATE, true);
    }

    public boolean getAddonsViewSourceEnabled() {
        return mPrefs.getBoolean(Name.ADDONS_VIEW_SOURCE, true);
    }

    public boolean getAnimEnabled() {
        return mPrefs.getBoolean(Name.ANIM, true);
    }

    public boolean getBlock3rdPartyCookiesEnabled() {
        return mPrefs.getBoolean(Name.BLOCK_3RD_PARTY_COOKIES, false);
    }

    public boolean getBlockImagesEnabled() {
        return mPrefs.getBoolean("blockimages", false);
    }

    public int getBookmarksStyle() {
        return mPrefs.getInt("bookmarksStye", 3);//原先默认是1
    }

    public int getChangelogCode() {
        return mPrefs.getInt("changelogcode", 1);
    }

    public boolean getClearCacheExit() {
        return mPrefs.getBoolean("cache", false);
    }

    public boolean getClearCookiesExitEnabled() {
        return mPrefs.getBoolean("clearCookiesExit", false);
    }

    public boolean getClearFormdataExitEnabled() {
        return mPrefs.getBoolean("clearFormdataExit", false);
    }

    public boolean getClearHistoryExitEnabled() {
        return mPrefs.getBoolean("clearHistoryExit", false);
    }

    public boolean getClearWebStorageExitEnabled() {
        return mPrefs.getBoolean("clearStorageExit", false);
    }

    public String getCode() {
        return mPrefs.getString(Name.CODE, "");
    }

    public boolean getColorModeEnabled() {
        return mPrefs.getBoolean(Name.COLOR_MODE, true);
    }

    public boolean getCookiesEnabled() {
        return mPrefs.getBoolean(Name.COOKIES, true);
    }

    public boolean getDoNotTrackEnabled() {
        return mPrefs.getBoolean("donottrack", true);
    }

    public String getDownloadDirectory() {
        return mPrefs.getString("download", Environment.DIRECTORY_DOWNLOADS);
    }

    public int getFlashSupport() {
        return mPrefs.getInt(Name.ADOBE_FLASH_SUPPORT, 0);
    }

    public boolean getFullScreenEnabled() {
        return mPrefs.getBoolean("fullscreen", false);
    }

    public int getGestureToolbarLeftChoice() {
        return mPrefs.getInt("gesturetoolbarleft", 12);
    }

    public int getGestureToolbarRightChoice() {
        return mPrefs.getInt("gesturetoolbarright", 13);
    }

    public boolean getHideStatusBarEnabled() {
        return mPrefs.getBoolean("hidestatus", false);
    }

    public boolean getHideUnderlineEnabled() {
        return mPrefs.getBoolean("hideunderline", true);
    }

    public String getHomepage() {
        return mPrefs.getString("home", "about:links");
    }

    public boolean getImageSwitchEnabled() {
        return mPrefs.getBoolean("imageswitch", false);
    }

    public boolean getIncognitoCookiesEnabled() {
        return mPrefs.getBoolean("incognitocookies", true);
    }

    public boolean getIncognitoModeEnabled() {
        return mPrefs.getBoolean("incognitoMode", false);
    }

    public static PreferenceManager getInstance() {
        if (mInstance == null) {
            mInstance = new PreferenceManager();
        }

        return mInstance;
    }

    public boolean getJavaScriptEnabled() {
        return mPrefs.getBoolean("java", true);
    }

    public int getKeyBackChoice() {
        return mPrefs.getInt("keyback", 2);
    }

    public int getKeyForwardChoice() {
        return mPrefs.getInt("keyforward", 3);
    }

    public int getKeyHomeChoice() {
        return mPrefs.getInt("keyhome", 4);
    }

    public int getKeyMenuChoice() {
        return mPrefs.getInt("keymenu", 1);
    }

    public int getKeyTabChoice() {
        return mPrefs.getInt("keytab", 5);
    }

    public int getLinksTimes() {
        return mPrefs.getInt("linkstimes", 0);
    }

    public boolean getLocalAdBlockEnabled() {
        return mPrefs.getBoolean("localAdb", true);
    }

    public boolean getLocationEnabled() {
        return mPrefs.getBoolean("location", false);
    }

    public boolean getLogInStatus() {
        return mPrefs.getBoolean("login", false);
    }

    public String getMemoryUrl() {
        return mPrefs.getString("memory", "");
    }

    public boolean getNightModeEnabled() {
        return mPrefs.getBoolean("nightmode", false);
    }

    public int getOpenTimes() {
        return mPrefs.getInt("opentimes", 0);
    }

    public boolean getOverviewModeEnabled() {
        return mPrefs.getBoolean("overviewmode", true);
    }

    public boolean getPopupsEnabled() {
        return mPrefs.getBoolean("newwindows", true);
    }

    public boolean getQuickBackEnabled() {
        return mPrefs.getBoolean("quickback", false);
    }

    public boolean getRemoveIdentifyingHeadersEnabled() {
        return mPrefs.getBoolean("removeidentifyingheaders", false);
    }

    public int getRenderingMode() {
        return mPrefs.getInt("renderMode", 0);
    }

    public boolean getRestoreLostTabsEnabled() {
        return mPrefs.getBoolean("restoreclosed", false);
    }

    public boolean getRuequstDesktopSiteEnabled() {
        return mPrefs.getBoolean("requestdesktopsite", false);
    }

    public boolean getSaveFormDataEnabled() {
        return mPrefs.getBoolean("saveformdata", true);
    }

    public boolean getSavePasswordsEnabled() {
        return mPrefs.getBoolean("passwords", true);
    }

    public int getScreenOrientation() {
        return mPrefs.getInt("screenOrientation", 1);
    }

    public int getSearchChoice() {
        return mPrefs.getInt("search", 1);
    }

    public boolean getSearchHintEnabled() {
        return mPrefs.getBoolean("searchhint", true);
    }

    public int getSearchTimes() {
        return mPrefs.getInt("searchtimes", 0);
    }

    public String getSearchUrl() {
        return mPrefs.getString("searchurl", Constants.GOOGLE_SEARCH);
    }

    public int getTextEncodingChoice() {
        return mPrefs.getInt("textencoding", 0);
    }

    public boolean getTextReflowEnabled() {
        return mPrefs.getBoolean("textreflow", false);
    }

    public int getTextSize() {
        return mPrefs.getInt("textsize", 3);
    }

    public int getUIChoice() {
        return mPrefs.getInt("ui", 0);
    }

    public int getUrlBoxChoice() {
        return mPrefs.getInt("urlbox", 0);
    }

    public boolean getUseWideViewportEnabled() {
        return mPrefs.getBoolean(Name.USE_WIDE_VIEWPORT, true);
    }

    public int getUserAgentChoice() {
        return mPrefs.getInt(Name.USER_AGENT, 1);
    }

    public String getUserAgentString(String def) {
        return mPrefs.getString("userAgentString", def);
    }

    public String getUserName() {
        return mPrefs.getString("username", "");
    }

    public String getUserPsw() {
        return mPrefs.getString("userpsw", "");
    }

    public boolean getVolumeScroll() {
        return mPrefs.getBoolean(Name.VOLUME, false);
    }

    public boolean getWakeLockEnabled() {
        return mPrefs.getBoolean(Name.WAKE_LOCK, false);
    }

    public boolean getWebIntentEnabled() {
        return mPrefs.getBoolean(Name.WEB_INTENT, true);
    }

    private void putBoolean(String name, boolean value) {
        mPrefs.edit().putBoolean(name, value).apply();
    }

    private void putInt(String name, int value) {
        mPrefs.edit().putInt(name, value).apply();
    }

    private void putString(String name, String value) {
        if (value != null) {
            mPrefs.edit().putString(name, value).apply();
        }
    }

    public void setAdBlockEnabled(boolean enable) {
        this.putBoolean(Name.BLOCK_ADS, enable);
    }

    public void setAddonsEnabled(String packageName, boolean enabled) {
        this.putBoolean(Name.ADDON + packageName, enabled);
    }

    public void setAddonsScreenshotEnabled(boolean enabled) {
        this.putBoolean(Name.ADDONS_SCREENSHOT, enabled);
    }

    public void setAddonsTranslateEnabled(boolean enabled) {
        this.putBoolean(Name.ADDONS_TRANSLATE, enabled);
    }

    public void setAddonsViewSourceEnabled(boolean enabled) {
        this.putBoolean(Name.ADDONS_VIEW_SOURCE, enabled);
    }

    public void setAnimEnabled(boolean enable) {
        this.putBoolean(Name.ANIM, enable);
    }

    public void setBlock3rdPartyCookiesEnabled(boolean enable) {
        this.putBoolean(Name.BLOCK_3RD_PARTY_COOKIES, enable);
    }

    public void setBlockImagesEnabled(boolean enable) {
        this.putBoolean("blockimages", enable);
    }

    public void setBookmarksStyle(int i) {
        this.putInt("bookmarksStye", i);
    }

    public void setChangelogCode(int i) {
        this.putInt("changelogcode", i);
    }

    public void setClearCacheExit(boolean enable) {
        this.putBoolean("cache", enable);
    }

    public void setClearCookiesExitEnabled(boolean enable) {
        this.putBoolean("clearCookiesExit", enable);
    }

    public void setClearFormdataExitEnabled(boolean enable) {
        this.putBoolean("clearFormdataExit", enable);
    }

    public void setClearHistoryExitEnabled(boolean enable) {
        this.putBoolean("clearHistoryExit", enable);
    }

    public void setClearWebStorageExitEnabled(boolean enable) {
        this.putBoolean("clearStorageExit", enable);
    }

    public void setCode(String code) {
        if (code != null && code.length() > 0) {
            this.putString(Name.CODE, code);
        }
    }

    public void setColorModeEnabled(boolean enabled) {
        this.putBoolean(Name.COLOR_MODE, enabled);
    }

    public void setCookiesEnabled(boolean enable) {
        this.putBoolean(Name.COOKIES, enable);
    }

    public void setDoNotTrackEnable(boolean enable) {
        this.putBoolean("donottrack", enable);
    }

    public void setDownloadDirectory(String directory) {
        this.putString("download", directory);
    }

    public void setFlashSupport(int n) {
        this.putInt(Name.ADOBE_FLASH_SUPPORT, n);
    }

    public void setFullScreenEnabled(boolean enable) {
        this.putBoolean("fullscreen", enable);
    }

    public void setGestureToolbarLeftChoice(int i) {
        this.putInt("gesturetoolbarleft", i);
    }

    public void setGestureToolbarRightChoice(int i) {
        this.putInt("gesturetoolbarright", i);
    }

    public void setHideStatusBarEnabled(boolean enable) {
        this.putBoolean("hidestatus", enable);
    }

    public void setHideUnderlineEnabled(boolean enable) {
        this.putBoolean("hideunderline", enable);
    }

    public void setHomepage(String homepage) {
        this.putString("home", homepage);
    }

    public void setImageSwitchEnabled(boolean enable) {
        this.putBoolean("imageswitch", enable);
    }

    public void setIncognitoCookiesEnabled(boolean enable) {
        this.putBoolean("incognitocookies", enable);
    }

    public void setIncognitoModeEnabled(boolean enable) {
        this.putBoolean("incognitoMode", enable);
    }

    public void setJavaScriptEnabled(boolean enable) {
        this.putBoolean("java", enable);
    }

    public void setKeyBackChoice(int i) {
        this.putInt("keyback", i);
    }

    public void setKeyForwardChoice(int i) {
        this.putInt("keyforward", i);
    }

    public void setKeyHomeChoice(int i) {
        this.putInt("keyhome", i);
    }

    public void setKeyMenuChoice(int i) {
        this.putInt("keymenu", i);
    }

    public void setKeyTabChoice(int i) {
        this.putInt("keytab", i);
    }

    public void setLinksTimes(int times) {
        this.putInt("linkstimes", times);
    }

    public void setLocalAdBlockEnabled(boolean enable) {
        this.putBoolean("localAdb", enable);
    }

    public void setLocationEnabled(boolean enable) {
        this.putBoolean("location", enable);
    }

    public void setLogInStatus(boolean enable) {
        this.putBoolean("login", enable);
    }

    public void setMemoryUrl(String url) {
        this.putString("memory", url);
    }

    public void setNightModeEnabled(boolean enable) {
        this.putBoolean("nightmode", enable);
    }

    public void setOpenTimes(int i) {
        this.putInt("opentimes", i);
    }

    public void setOverviewModeEnabled(boolean enable) {
        this.putBoolean("overviewmode", enable);
    }

    public void setPopupsEnabled(boolean enable) {
        this.putBoolean("newwindows", enable);
    }

    public void setQuickBackEnabled(boolean enabled) {
        this.putBoolean("quickback", enabled);
    }

    public void setRemoveIdentifyingHeadersEnabled(boolean enable) {
        this.putBoolean("removeidentifyingheaders", enable);
    }

    public void setRenderingMode(int mode) {
        this.putInt("renderMode", mode);
    }

    public void setRestoreLostTabsEnabled(boolean enable) {
        this.putBoolean("restoreclosed", enable);
    }

    public void setRuequstDesktopSiteEnabled(boolean enable) {
        this.putBoolean("requestdesktopsite", enable);
    }

    public void setSaveFormdataEnabled(boolean enable) {
        this.putBoolean("saveformdata", enable);
    }

    public void setSavePasswordsEnabled(boolean enable) {
        this.putBoolean("passwords", enable);
    }

    public void setScreenOrientation(int mode) {
        this.putInt("screenOrientation", mode);
    }

    public void setSearchChoice(int choice) {
        this.putInt("search", choice);
    }

    public void setSearchHintEnabled(boolean enable) {
        this.putBoolean("searchhint", enable);
    }

    public void setSearchTimes(int times) {
        this.putInt("searchtimes", times);
    }

    public void setSearchUrl(String url) {
        this.putString("searchurl", url);
    }

    public void setTextEncodingChoice(int choice) {
        this.putInt("textencoding", choice);
    }

    public void setTextReflowEnabled(boolean enable) {
        this.putBoolean("textreflow", enable);
    }

    public void setTextSize(int size) {
        this.putInt("textsize", size);
    }

    public void setUIChoice(int choice) {
        this.putInt("ui", choice);
    }

    public void setUrlBoxChoice(int choice) {
        this.putInt("urlbox", choice);
    }

    public void setUseWideViewportEnabled(boolean enable) {
        this.putBoolean(Name.USE_WIDE_VIEWPORT, enable);
    }

    public void setUserAgentChoice(int choice) {
        this.putInt(Name.USER_AGENT, choice);
    }

    public void setUserAgentString(String agent) {
        this.putString("userAgentString", agent);
    }

    public void setUserName(String name) {
        this.putString("username", name);
    }

    public void setUserPsw(String psw) {
        this.putString("userpsw", psw);
    }

    public void setVolumeScrollEnabled(boolean enable) {
        this.putBoolean(Name.VOLUME, enable);
    }

    public void setWakeLockEnabled(boolean enable) {
        this.putBoolean(Name.WAKE_LOCK, enable);
    }

    public void setWebIntentEnabled(boolean enable) {
        this.putBoolean(Name.WEB_INTENT, enable);
    }
}

